package com.nowcoder.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.nowcoder.entity.DiscussPost;
import com.nowcoder.mapper.DiscussPostMapper;
import com.nowcoder.service.DiscussPostService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nowcoder.utils.SensitiveFilter;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.util.HtmlUtils;

import java.util.List;

/**
 * <p>
 * 讨论帖信息表 服务实现类
 * </p>
 *
 * @author cd
 * @since 2021-12-18
 */
@Service
public class DiscussPostServiceImpl extends ServiceImpl<DiscussPostMapper, DiscussPost> implements DiscussPostService {

    @Autowired
    private SensitiveFilter sensitiveFilter;

    @Override
    public int insertDiscussPost(DiscussPost discussPost) {
        //转义HTML标记
        discussPost.setTitle(HtmlUtils.htmlEscape(discussPost.getTitle()));
        discussPost.setContent(HtmlUtils.htmlEscape(discussPost.getContent()));
        //过滤敏感词
        discussPost.setTitle(sensitiveFilter.filter(discussPost.getTitle()));
        discussPost.setContent(sensitiveFilter.filter(discussPost.getContent()));

        return baseMapper.insert(discussPost);
    }

    @Override
    public List<DiscussPost> selectDiscussPosts(String uid, int offset, int limit) {
        List<DiscussPost> discussPosts = null;
        if (uid == null) {
            discussPosts = baseMapper.selectList(new QueryWrapper<DiscussPost>()
                    .ne("status", 2)
                    .orderByDesc("type")
                    .orderByDesc("create_time"));
        } else {
            discussPosts = baseMapper.selectList(new QueryWrapper<DiscussPost>()
                    .eq("uid", uid)
                    .ne("status", 2)
                    .orderByDesc("type")
                    .orderByDesc("create_time"));
        }
        limit = Math.min(discussPosts.size(), limit);

        return discussPosts.subList(offset, limit);
    }

    @Override
    public int selectDiscussPostRows(@Param("uid") String uid) {
        Integer count = null;
        if (uid == null) {
            count = baseMapper.selectCount(new QueryWrapper<>());
        } else {
            count = baseMapper.selectCount(new QueryWrapper<DiscussPost>()
                    .eq("uid", uid));
        }
//        count = baseMapper.selectCount(new QueryWrapper<DiscussPost>()
//                .isNotNull(uid)
//                .eq("uid", uid));

        return count == null ? 0 : count;
    }

    @Override
    public DiscussPost selectDiscussPostById(int id) {
        return baseMapper.selectById(id);
    }

    @Override
    public int updateCommentCount(int id, int commentCount) {
        return baseMapper.update(null, new UpdateWrapper<DiscussPost>().eq("id", id).set("comment_count", commentCount));
    }
}
